package topin.facecat.FaceCat.UI;

import topin.facecat.FaceCat.Service.Security;
import topin.facecat.FaceCat.Service.SecurityLatestData;
import facecat.topin.core.*;
import java.util.*;

public class LatestDiv extends FCView {
    public LatestDiv()
    {
        setBorderColor(FCColor.None);
    }

    public int m_digit = 2;

    private SecurityLatestData m_latestData = new SecurityLatestData();

    public SecurityLatestData getLatestData()
    {
        return m_latestData;
    }

    public boolean m_lV2;

    public String m_code;

    public void setCode(String value)
    {
        m_code = value;
        Security security = new Security();
        if (DataCenter.m_securityService.getSecurityByCode(m_code, security))
        {
            m_isIndex = security.m_type == 0;
        }
        else
        {
            m_isIndex = false;
        }
    }

    public boolean m_isIndex;

    public String m_sName;

    public void drawValue(FCPaint paint, double value, int digit, FCFont font, long color, int x, int y)
    {
        if (value == m_latestData.m_upperLimit)
        {
            FCSize pSize = paint.textSize(FCTran.getValueByDigit(value, digit), font, -1);
            FCRect bkRect = new FCRect(x, y, x + pSize.cx, y + pSize.cy);
            bkRect.right += 1;
            paint.fillRect(FCColor.rgb(255, 82, 82), bkRect);
            FCDraw.drawUnderLineNum2(paint, value, digit, font, FCColor.Back, true, x, y);
        }
        else if (value == m_latestData.m_lowerLimit)
        {
            FCSize pSize = paint.textSize(FCTran.getValueByDigit(value, digit), font, -1);
            FCRect bkRect = new FCRect(x, y, x + pSize.cx, y + pSize.cy);
            bkRect.right += 1;
            paint.fillRect(FCColor.rgb(82, 255, 82), bkRect);
            FCDraw.drawUnderLineNum2(paint, value, digit, font, FCColor.Back, true, x, y);
        }
        else
        {
            FCDraw.drawUnderLineNum2(paint, value, digit, font, color, true, x, y);
        }
    }

    public void onPaintForeground(FCPaint paint, FCRect clipRect)
    {
        int width = getWidth();
        int height = getHeight();
        if (width > 0 && height > 0)
        {
            FCFont font = new FCFont("Default", 14, false, false, false);
            FCFont lfont = new FCFont("Default", 12, false, false, false);
            long wordColor = MyColor.USERCOLOR218;
            int top = 32, step = 20;
            if (m_isIndex)
            {
                font.m_fontSize = 14;
                FCDraw.drawText(paint, "现价", wordColor, font, 1, top);
                FCDraw.drawText(paint, "幅度", wordColor, font, 1, top + 20);
                FCDraw.drawText(paint, "总量", wordColor, font, 1, top + 40);
                FCDraw.drawText(paint, "总额", wordColor, font, 1, top + 60);
                FCDraw.drawText(paint, "开盘", wordColor, font, 110, top);
                FCDraw.drawText(paint, "最高", wordColor, font, 110, top + 20);
                FCDraw.drawText(paint, "最低", wordColor, font, 110, top + 40);
                FCDraw.drawText(paint, "振幅", wordColor, font, 110, top + 60);
                //画股票代码
                long yellowColor = FCColor.rgb(230, 230, 230);
                if (MyColor.m_style == 1)
                {
                    yellowColor = FCColor.Text;
                }
                if (m_latestData.m_code != null && m_latestData.m_code.length() > 0)
                {
                    long color = 0;
                    double close = m_latestData.m_close, open = m_latestData.m_open, high = m_latestData.m_high, low = m_latestData.m_low, lastClose = m_latestData.m_lastClose;
                    if (close == 0)
                    {
                        if (m_latestData.m_buyPrice1 > 0)
                        {
                            close = m_latestData.m_buyPrice1;
                        }
                        else if (m_latestData.m_sellPrice1 > 0)
                        {
                            close = m_latestData.m_sellPrice1;
                        }
                    }
                    if (lastClose == 0)
                    {
                        lastClose = close;
                    }
                    color = FCDraw.getPriceColor(close, lastClose);
                    FCDraw.drawUnderLineNum(paint, close, m_digit, font, color, true, 35, top);
                    //升跌
                    double sub = 0;
                    if (close == 0)
                    {
                        sub = m_latestData.m_buyPrice1 - lastClose;
                        double rate = 100 * (m_latestData.m_buyPrice1 - lastClose) / lastClose;
                        if (m_latestData.m_buyPrice1 == 0)
                        {
                            rate = 0;
                            sub = 0;
                        }
                        color = FCDraw.getPriceColor(rate, 0);
                        int pleft = FCDraw.drawUnderLineNum(paint, rate, 2, font, color, false, 35, top + 20);
                        FCDraw.drawText(paint, "%", color, font, pleft + 37, top + 20);
                    }
                    else
                    {
                        color = FCDraw.getPriceColor(close, lastClose);
                        sub = close - m_latestData.m_lastClose;
                        double rate = 100 * (close - lastClose) / lastClose;
                        int pleft = FCDraw.drawUnderLineNum(paint, rate, 2, font, color, false, 35, top + 20);
                        FCDraw.drawText(paint, "%", color, font, pleft + 37, top + 20);
                    }
                    color = FCDraw.getPriceColor(open, lastClose);
                    FCDraw.drawUnderLineNum(paint, open, m_digit, font, color, true, 145, top);

                    double volume = m_latestData.m_volume / 100;
                    String unit = "";
                    if (volume > 10000000)
                    {
                        volume /= 10000000;
                        unit = "亿";
                    }
                    else if (volume > 10000)
                    {
                        volume /= 10000;
                        unit = "万";
                    }
                    String unit2 = "";
                    double amount = m_latestData.m_amount / 100000000;
                    unit2 = "亿";
                    //总手
                    int cleft = FCDraw.drawUnderLineNum(paint, volume, unit.length() > 0 ? m_digit : 0, font, yellowColor, true, 35, top + 40);
                    if (unit.length() > 0)
                    {
                        FCDraw.drawText(paint, unit, yellowColor, font, cleft + 37, top + 40);
                    }
                    color = FCDraw.getPriceColor(high, lastClose);
                    FCDraw.drawUnderLineNum(paint, high, m_digit, font, color, true, 145, top + 20);
                    cleft = FCDraw.drawUnderLineNum(paint, amount, unit2.length() > 0 ? m_digit : 0, font, yellowColor, true, 35, top + 60);
                    if (unit2.length() > 0)
                    {
                        FCDraw.drawText(paint, unit2, yellowColor, font, cleft + 37, top + 60);
                    }
                    color = FCDraw.getPriceColor(low, lastClose);
                    FCDraw.drawUnderLineNum(paint, low, m_digit, font, color, true, 145, top + 40);
                    double range = 0;
                    if (lastClose != 0)
                    {
                        range = 100 * (m_latestData.m_high - lastClose) / lastClose - 100 * (m_latestData.m_low - lastClose) / lastClose;
                    }
                    cleft = FCDraw.drawUnderLineNum(paint, range, 2, font, yellowColor, true, 145, top + 60);
                    if (range > 0)
                    {
                        FCDraw.drawText(paint, "%", yellowColor, font, cleft + 147, top + 60);
                    }
                }
            }
            else
            {
                //画买卖盘
                FCDraw.drawText(paint, "卖", wordColor, font, 1, (m_lV2 ? 87 : 47));
                FCDraw.drawText(paint, "盘", wordColor, font, 1, (m_lV2 ? 140 : 100));
                FCDraw.drawText(paint, "买", wordColor, font, 1, (m_lV2 ? 267 : 147));
                FCDraw.drawText(paint, "盘", wordColor, font, 1, (m_lV2 ? 310 : 200));
                String buySellStr = "5,4,3,2,1,1,2,3,4,5";
                if (m_lV2)
                {
                    step = 16;
                    buySellStr = "总卖量,10,9,8,7,6," + buySellStr + ",6,7,8,9,10,总买量";
                    font.m_fontSize = 14;
                }
                String[] buySellStrs = buySellStr.split("[,]");
                int strsSize = buySellStrs.length;
                for (int i = 0; i < strsSize; i++)
                {
                    FCDraw.drawText(paint, buySellStrs[i], wordColor, font, 25, top);
                    top += step;
                }
                font.m_fontSize = 14;
                top = m_lV2 ? 390 : 232;
                FCDraw.drawText(paint, "现价", wordColor, font, 1, top);
                FCDraw.drawText(paint, "幅度", wordColor, font, 1, top + 20);
                FCDraw.drawText(paint, "总量", wordColor, font, 1, top + 40);
                FCDraw.drawText(paint, "总额", wordColor, font, 1, top + 60);
                FCDraw.drawText(paint, "换手", wordColor, font, 1, top + 80);
                FCDraw.drawText(paint, "PE", wordColor, font, 1, top + 100);
                FCDraw.drawText(paint, "股本", wordColor, font, 1, top + 120);
                FCDraw.drawText(paint, "流通", wordColor, font, 1, top + 140);
                FCDraw.drawText(paint, "均价", wordColor, font, 110, top);
                FCDraw.drawText(paint, "开盘", wordColor, font, 110, top + 20);
                FCDraw.drawText(paint, "最高", wordColor, font, 110, top + 40);
                FCDraw.drawText(paint, "最低", wordColor, font, 110, top + 60);
                FCDraw.drawText(paint, "振幅", wordColor, font, 110, top + 80);
                FCDraw.drawText(paint, "量比", wordColor, font, 110, top + 100);
                FCDraw.drawText(paint, "市值", wordColor, font, 110, top + 120);
                FCDraw.drawText(paint, "流值", wordColor, font, 110, top + 140);
                //画股票代码
                long yellowColor = FCColor.rgb(190, 190, 235);
                if (MyColor.m_style == 1)
                {
                    yellowColor = FCColor.Text;
                }
                if (m_latestData.m_code != null && m_latestData.m_code.length() > 0)
                {
                    double close = m_latestData.m_close, open = m_latestData.m_open, high = m_latestData.m_high, low = m_latestData.m_low, lastClose = m_latestData.m_lastClose;
                    if (close == 0)
                    {
                        if (m_latestData.m_buyPrice1 > 0)
                        {
                            close = m_latestData.m_buyPrice1;
                        }
                        else if (m_latestData.m_sellPrice1 > 0)
                        {
                            close = m_latestData.m_sellPrice1;
                        }
                    }
                    if (lastClose == 0)
                    {
                        lastClose = close;
                    }
                    ArrayList<Double> plist = new ArrayList<Double>();
                    ArrayList<Double> vlist = new ArrayList<Double>();
                    double multi = 100;
                    plist.add(m_latestData.m_sellPrice5);
                    plist.add(m_latestData.m_sellPrice4);
                    plist.add(m_latestData.m_sellPrice3);
                    plist.add(m_latestData.m_sellPrice2);
                    plist.add(m_latestData.m_sellPrice1);
                    vlist.add(m_latestData.m_sellVolume5 / multi);
                    vlist.add(m_latestData.m_sellVolume4 / multi);
                    vlist.add(m_latestData.m_sellVolume3 / multi);
                    vlist.add(m_latestData.m_sellVolume2 / multi);
                    vlist.add(m_latestData.m_sellVolume1 / multi);
                    plist.add(m_latestData.m_buyPrice1);
                    plist.add(m_latestData.m_buyPrice2);
                    plist.add(m_latestData.m_buyPrice3);
                    plist.add(m_latestData.m_buyPrice4);
                    plist.add(m_latestData.m_buyPrice5);
                    vlist.add(m_latestData.m_buyVolume1 / multi);
                    vlist.add(m_latestData.m_buyVolume2 / multi);
                    vlist.add(m_latestData.m_buyVolume3 / multi);
                    vlist.add(m_latestData.m_buyVolume4 / multi);
                    vlist.add(m_latestData.m_buyVolume5 / multi);
                    long color = 0;
                    double mx = 0;
                    for (int i = 0; i < vlist.size(); i++)
                    {
                        if (i == 0)
                        {
                            mx = vlist.get(i);
                        }
                        else
                        {
                            if (mx < vlist.get(i))
                            {
                                mx = vlist.get(i);
                            }
                        }
                    }
                    font.m_fontSize = m_lV2 ? 14 : 14;
                    if (mx > 0)
                    {
                        //绘制买卖盘
                        int pLength = plist.size();
                        top = 32;
                        if (m_lV2)
                        {
                            top += step;
                        }
                        for (int i = 0; i < pLength; i++)
                        {
                            color = FCDraw.getPriceColor(plist.get(i), lastClose);
                            drawValue(paint, plist.get(i), m_digit, font, color, m_lV2 ? 80 : 60, top);
                            if (i < 5)
                            {
                                color = FCDraw.getPriceColor(0.5, 1);
                            }
                            else
                            {
                                color = FCDraw.getPriceColor(1, 0.5);
                            }
                            FCDraw.drawUnderLineNum2(paint, vlist.get(i), 0, font, color, true, m_lV2 ? 130 : 110, top);
                            //paint.fillRect(color, new FCRect(width - (int)(vlist[i] / mx * 50), top + step / 2 - 2, width, top + step / 2 + 2));
                            top += step;
                        }
                        if (m_lV2)
                        {
                            top += step;
                        }
                    }
                    vlist.clear();
                    plist.clear();
                    top = m_lV2 ? 390 : 232;
                    //成交
                    color = FCDraw.getPriceColor(close, lastClose);
                    drawValue(paint, close, m_digit, font, color, 35, top);
                    //均价
                    if (m_latestData.m_volume > 0)
                    {
                        double avgPrice = m_latestData.m_amount / m_latestData.m_volume;
                        color = FCDraw.getPriceColor(avgPrice, lastClose);
                        drawValue(paint, avgPrice, m_digit, font, color, 145, top);
                    }
                    else
                    {
                        FCDraw.drawText(paint, "-", wordColor, font, 145, top);
                    }
                    //升跌
                    double sub = 0;
                    if (close == 0)
                    {
                        sub = m_latestData.m_buyPrice1 - lastClose;
                        double rate = 100 * (m_latestData.m_buyPrice1 - lastClose) / lastClose;
                        if (m_latestData.m_buyPrice1 == 0)
                        {
                            rate = 0;
                            sub = 0;
                        }
                        color = FCDraw.getPriceColor(rate, 0);
                        int pleft = FCDraw.drawUnderLineNum(paint, rate, 2, font, color, false, 35, top + 20);
                        FCDraw.drawText(paint, "%", color, font, pleft + 37, top + 20);
                    }
                    else
                    {
                        color = FCDraw.getPriceColor(close, lastClose);
                        sub = close - m_latestData.m_lastClose;
                        double rate = 100 * (close - lastClose) / lastClose;
                        int pleft = FCDraw.drawUnderLineNum(paint, rate, 2, font, color, false, 35, top + 20);
                        FCDraw.drawText(paint, "%", color, font, pleft + 37, top + 20);
                    }
                    color = FCDraw.getPriceColor(open, lastClose);
                    drawValue(paint, open, m_digit, font, color, 145, top + 20);

                    double volume = m_latestData.m_volume / 100;
                    String unit = "";
                    if (volume > 10000000)
                    {
                        volume /= 10000000;
                        unit = "亿";
                    }
                    else if (volume > 10000)
                    {
                        volume /= 10000;
                        unit = "万";
                    }
                    String unit2 = "";
                    double amount = m_latestData.m_amount / 100000000;
                    unit2 = "亿";
                    //总手
                    int cleft = FCDraw.drawUnderLineNum(paint, volume, unit.length() > 0 ? m_digit : 0, font, yellowColor, true, 35, top + 40);
                    if (unit.length() > 0)
                    {
                        FCDraw.drawText(paint, unit, yellowColor, font, cleft + 37, top + 40);
                    }
                    color = FCDraw.getPriceColor(high, lastClose);
                    drawValue(paint, high, m_digit, font, color, 145, top + 40);
                    cleft = FCDraw.drawUnderLineNum(paint, amount, unit2.length() > 0 ? m_digit : 0, font, yellowColor, true, 35, top + 60);
                    if (unit2.length() > 0)
                    {
                        FCDraw.drawText(paint, unit2, yellowColor, font, cleft + 37, top + 60);
                    }
                    color = FCDraw.getPriceColor(low, lastClose);
                    drawValue(paint, low, m_digit, font, color, 145, top + 60);
                    double turnoverRate = m_latestData.m_turnoverRate;
                    cleft = FCDraw.drawUnderLineNum(paint, turnoverRate, 2, font, yellowColor, false, 35, top + 80);
                    FCDraw.drawText(paint, "%", yellowColor, font, cleft + 37, top + 80);
                    double range = 0;
                    if (lastClose != 0)
                    {
                        range = 100 * (m_latestData.m_high - lastClose) / lastClose - 100 * (m_latestData.m_low - lastClose) / lastClose;
                    }
                    cleft = FCDraw.drawUnderLineNum(paint, range, 2, font, yellowColor, true, 145, top + 80);
                    if (range > 0)
                    {
                        FCDraw.drawText(paint, "%", yellowColor, font, cleft + 147, top + 80);
                    }
                    if (m_latestData.m_pe < 0)
                    {
                        FCDraw.drawText(paint, "亏损", yellowColor, font, 35, top + 100);
                    }
                    else
                    {
                        FCDraw.drawUnderLineNum(paint, m_latestData.m_pe, m_digit, font, yellowColor, true, 35, top + 100);
                    }
                    FCDraw.drawUnderLineNum(paint, m_latestData.m_ratio, m_digit, font, yellowColor, true, 145, top + 100);
                    double equity = m_latestData.m_equity;

                    if (equity > 100000000)
                    {
                        equity /= 100000000;
                        unit = "亿";
                    }
                    else if (equity > 10000)
                    {
                        equity /= 10000;
                        unit = "万";
                    }
                    cleft = FCDraw.drawUnderLineNum(paint, equity, unit.length() > 0 ? m_digit : 0, font, yellowColor, true, 35, top + 120);
                    if (unit.length() > 0)
                    {
                        FCDraw.drawText(paint, unit, yellowColor, font, cleft + 37, top + 120);
                    }
                    double flowEquity = m_latestData.m_flowEquity;
                    if (flowEquity > 100000000)
                    {
                        flowEquity /= 100000000;
                        unit = "亿";
                    }
                    else if (flowEquity > 10000)
                    {
                        flowEquity /= 10000;
                        unit = "万";
                    }
                    cleft = FCDraw.drawUnderLineNum(paint, flowEquity, unit.length() > 0 ? m_digit : 0, font, yellowColor, true, 35, top + 140);
                    if (unit.length() > 0)
                    {
                        FCDraw.drawText(paint, unit, yellowColor, font, cleft + 37, top + 140);
                    }
                    cleft = FCDraw.drawUnderLineNum(paint, m_latestData.m_marketValue / 100000000, 2, font, yellowColor, true, 145, top + 120);
                    String unit3 = "亿";
                    if (unit3.length() > 0)
                    {
                        FCDraw.drawText(paint, unit3, yellowColor, font, cleft + 147, top + 120);
                    }
                    cleft = FCDraw.drawUnderLineNum(paint, m_latestData.m_flow / 100000000, 2, font, yellowColor, true, 145, top + 140);
                    if (unit3.length() > 0)
                    {
                        FCDraw.drawText(paint, unit3, yellowColor, font, cleft + 147, top + 140);
                    }
                }
                paint.drawLine(FCColor.Border, 1, 0, 0, 0, 0, height);
                paint.drawLine(FCColor.Border, 1, 0, 0, 30, width, 30);
                paint.drawLine(FCColor.Border, 1, 0, 24, 30, 24, top - 2);
                paint.drawLine(FCColor.Border, 1, 0, 0, 131, width, 131);
                paint.drawLine(FCColor.Border, 1, 0, 0, top - 2, width, top - 2);
                paint.drawLine(FCColor.Border, 1, 0, width, 0, width, height);
            }
            font.m_bold = false;
            font.m_fontSize = 14;
            //股票代码
            if (m_code != null && m_code.length() > 0)
            {
                String showCode = m_code.substring(0, m_code.indexOf("."));
                FCDraw.drawText(paint, showCode, MyColor.USERCOLOR218, font, 5, 4);
            }
            //股票名称
            if (m_sName != null && m_sName.length() > 0)
            {
                FCDraw.drawText(paint, m_sName, MyColor.USERCOLOR217, font, 55, 4);
            }
        }
    }

    public void bindData(SecurityLatestData latestData, boolean refresh)
    {
        m_latestData = latestData;
        setCode(m_latestData.m_code);
        Security security = new Security();
        if (DataCenter.m_securityService.getSecurityByCode(m_latestData.m_code, security))
        {
            m_sName = security.m_name;
        }
        else
        {
            m_sName = "";
        }
        if (refresh)
        {
            invalidate();
        }
    }
}

